Doing Jenkins Like A Hacker

    

 

 

Exploring Jenkins

 

Jenkins was one of the most popular CI tools used to build and test different kinds of projects. It's now also used for CD and being the very important part in DevOps practices.

Firstly, we need to understand what CI is. CI is one of the most popular application development practices in recent times. Developers integrate new feature, bug fixes in a code repository. Then CI tool verifies automatically build and test to detect any problem with current sources and provide quick feedback to development team.

"Jenkins is an open source automation server written in Java. Jenkins helps to automate the non-human part of the software developmentprocess, with continuous integration and facilitating technical aspects of continuous delivery. It is a server-based system that runs in servlet containers such as Apache TomcatWikipedia

 

 

 

 

Jenkins plugins

 

Plugins make Jenkins more easy to learn, easy to use. There are various categories of plugins avaiable for Jenkins, such as:

Source code management

Slave launchers and controllers

Build triggers

Build tools

Build notifiers

Build report

Post build actions

UI plugins

Library plugins

 

In this article, I'm using Laravel project with Git repo and Java e2e automation test for example build. So there is should-have plugins:

 

1.   Jenkins Pipeline plugin

2.   SonarQube

3.   Github Integration plugin

4.   Allure Jenkins Plugin

5.    Default suggestion plugin from Jenkins

 

Pipeline as code concept

 

The default interaction model with Jenkins, historically, has been very web UI driven, requiring users to manually create jobs, then manually fill in the details through a web browser. This requires additional effort to create and manage jobs to test and build multiple projects, it also keeps the configuration of a job to build/test/deploy separate from the actual code being built/tested/deployed. This prevents users from applying their existing CI/CD best practices to the job configurations themselves.

With Jenkins pipeline plugin as I listed in should-have plugins, users can implement a project's entire build-test-deploy pipeline in a Jenkinsfile and store that alongside their code.

Building on the core Jenkins value of extensibility, Pipeline is also extensible both by users with Pipeline Shared Libraries and by plugin developers. 

The flowchart below is an example of one CD scenario easily modeled in Jenkins Pipeline:

Pipeline syntax overview:

There are 2 kinds of pipeline code:

·      Declarative Pipeline (New concept):

 

Declarative Pipeline replaces Groovy variable assignments, control structures, loops, and exception handling with a predefined structure and model to allow users of all experience levels to quickly create consistent, concise Pipelines without learning Groovy.

 All valid Declarative Pipelines must be enclosed within a pipeline block,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

·       Scripted Pipeline (Advance):

 

 

Scripted Pipeline, like Declarative Pipeline, is built on top of the underlying Pipeline sub-system. Unlike Declarative, Scripted Pipeline is effectively a general-purpose DSL built with Groovy. Most functionality provided by the Groovy language is made available to users of Scripted Pipeline, which means it can be a very expressive and flexible tool with which one can author continuous delivery pipelines.

 

 

 

 

 

In this article, I can’t give you a fully introduction for Pipeline syntax. If you want to get deeper with this, this docs contains fully syntax and examples for both ways of pipeline code.

 

 

 

 

 

 

Jenkins in Actions

There are a lot of way to approach Jenkins. You still can get your job done perfectly even you are using Web UI driven or Pipeline as Code. Base on your organization, projects and skills, you can choose what is most suitable for yourself.

In this section, I’m introducing how did I implement CI/CD server for my organization.

1.   Requirements:

a.   Implement CI for automated build Laravel, Rails projects

b.   PHP code should be following PS-R2 coding standard.

c.    Execute e2e test written in Java

2.   Actions

a.   For building project, I prefer using docker container and execute bash script inside.

b.   For checking Code quality, SonaQube is my best choice

c.    Following requirement, I create a skeleton for project:

 

d.   Then I filled up actions for each defined stage in Jenkinsfile for a laravel project:

From these code, we can see BRANCH_NAME and CREDENTIAL_GITHUB is input variable from Jenkins project, so we need to config some more things:

 

                 

 

 

 

3.   Refactor

Let’s do some refactor for this.

Firstly, we need to implement this pipeline for more than one project, so reuse pipeline code and keep it D.R.Y should be a good way for saving development time.

Pipeline has support for creating "Shared Libraries" which can be defined in external source control repositories and loaded into existing Pipelines.\\

1.   Defining Shared Library struture:

2.   Phppipeline.groovy file:

a.   To deploy shared library, we need to push pipeline code into a repository then go to Manage Jenkins and Add new Global Pipeline Libraries.

 

 

b.   So now, Jenkins file for project become like this:

 

 

Conclusion

     

I hope that I’ve showed you the most common tasks for implementing CI/CD with Jenkins.

There are some important things that we should remember:

o   Jenkins is automation server that used for implementing CI/CD following DevOps practice.

o   Jenkins has various plugin which help we can do our job simpler

o   Jenkins pipeline as code help we can create and reuse pipeline for a group of projects

o   Using Jenkins Shared library would save our time, keep our Jenkins code “DRY”, simpler.